package com.leyou.page.listener;

import com.leyou.common.constants.MQConstants;
import com.leyou.common.constants.MQConstants.RoutingKey;
import com.leyou.page.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.MQConstants.Queue.PAGE_ITEM_DOWN;
import static com.leyou.common.constants.MQConstants.Queue.PAGE_ITEM_UP;

@Component
public class PageListener {
    @Autowired
    PageService pageService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = PAGE_ITEM_UP, declare = "true"),
            exchange = @Exchange(value = MQConstants.Exchange.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = RoutingKey.ITEM_UP_KEY))
    public void addPage(Long id) {
        pageService.creatItemHtml(id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = PAGE_ITEM_DOWN, declare = "true"),
            exchange = @Exchange(value = MQConstants.Exchange.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = RoutingKey.ITEM_DOWN_KEY))
    public void deletePage(Long id) {
        pageService.deleteItemHtml(id);
    }
}
